package cn.iocoder.yudao.module.handguard.tool;

import cn.iocoder.yudao.module.handguard.controller.app.orderprocessrecord.vo.OrderProcessRecordRespVO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.productaccessitem.ProductAccessItemDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.usertablesaudit.UserTablesAuditDO;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author liuweijun
 * @version 1.0
 * @className DataConversionUtils (此处以class为例)
 * @date 5/22/24
 * @description TODO
 */
public class DataConversionUtils {

    /**
     * 使用stream的groupingBy分组
     *
     * @param tablesList
     * @return
     */
    public static Map<String, List<UserTablesAuditDO>> groupByStream(List<UserTablesAuditDO> tablesList) {
        // 使用LinkedHashMap来保持插入顺序
        Map<String, List<UserTablesAuditDO>> result = new LinkedHashMap<>();

        // 遍历原始列表，按typeName分组并保持顺序
        for (UserTablesAuditDO table : tablesList) {
            if (table != null) {
                String typeName = table.getTypeName();
                result.computeIfAbsent(typeName, k -> new ArrayList<>()).add(table);
            }
        }
        return result;
//        return tablesList.stream().filter(Objects::nonNull)
//                .collect(Collectors.groupingBy(UserTablesAuditDO::getTypeName));
    }


    /**
     * 使用stream的groupingBy分组
     *
     * @param tablesList
     * @return
     */
    public static Map<String, List<OrderProcessRecordRespVO>> groupOrderProcessByStream(List<OrderProcessRecordRespVO> tablesList) {
        return tablesList.stream().filter(Objects::nonNull)
                .collect(Collectors.groupingBy(OrderProcessRecordRespVO::getFlowN));
    }


    /**
     * 使用stream的groupingBy分组
     *
     * @param tablesList
     * @return
     */
    public static Map<String, List<ProductAccessItemDO>> groupProductAccessByStream(List<ProductAccessItemDO> tablesList) {
        return tablesList.stream().filter(Objects::nonNull)
                .collect(Collectors.groupingBy(ProductAccessItemDO::getTypeNames));
    }

    //包含，但排除用户是无
    public static Boolean getCommonCheck62(String v1, String v2) {
        boolean flag = false;
        String[] arr = v1.split(",");
        endLoop:
        for (String s : arr) {
            if (v2.contains(s) && !v2.equals("无")) {
                flag = true;
                break endLoop;
            }
        }
        return flag;
    }



    //X<=B and Y>=B
    public static Boolean getCommonCheck200(String v1, String v2) {
        boolean flag = false;
        Double b = Double.valueOf(StringUtils.getDigits(v2));
        if (v1.startsWith("-1")){
            String[] parts = v1.split("-(?=.*-)");
            String p = parts[1];
            String one = "-1";
            String two = p.split("-")[1];
            Double a1 = Double.valueOf(StringUtils.getDigits(one));
            Double a2 = Double.valueOf(StringUtils.getDigits(two));
            if (a1<=b && a2>=b) {
                flag = true;
            }
        }else {
            String[] arr = v1.split("-");
            Double a1 = Double.valueOf(StringUtils.getDigits(arr[0]));
            Double a2 = Double.valueOf(StringUtils.getDigits(arr[1]));
            if (a1<=b && a2>=b) {
                flag = true;
            }
        }

        return flag;
    }

    /**
     * 连续逾期：B＜A1 累计逾期：B＜A2
     * 新
     * @param v1
     * @param v2
     * @return
     */
    public static Boolean getCommonCheck201(String v1, String v2) {

        boolean flag = false;
        if (StringUtils.isNotEmpty(v1) && StringUtils.isNotEmpty(v2)) {
            String[] arr = v1.split(",");
            String[] lxScope = arr[0].split("-");
            //String[] ljScope = arr[1].split("-");
            Double a1 = Double.valueOf(StringUtils.getDigits(lxScope[0]));
            Double a2 = Double.valueOf(StringUtils.getDigits(lxScope[1]));
//        Double ljScope3 = Double.valueOf(StringUtils.getDigits(ljScope[0]));
//        Double ljScope4 = Double.valueOf(StringUtils.getDigits(ljScope[1]));
            String[] bArr = v2.split("-");
            Double b1 = Double.valueOf(StringUtils.getDigits(bArr[0]));
            Double b2 = Double.valueOf(StringUtils.getDigits(bArr[1]));
            if (b1<a1 && b2<a2) {
                flag = true;
            }
        }
        return flag;
    }

    //X<=B连续逾期 and Y>B连续逾期） and （F<=累计逾期 and H>B累计逾期）（暂时未用）
    public static Boolean getCommonCheck202(String v1, String v2) {
        boolean flag = false;

        if (StringUtils.isNotEmpty(v1) && StringUtils.isNotEmpty(v2)) {
            String[] arr = v1.split(",");
            String[] lxScope = arr[0].split("-");
            String[] ljScope = arr[1].split("-");
            Double lxScope1 = Double.valueOf(StringUtils.getDigits(lxScope[0]));
            Double lxScope2 = Double.valueOf(StringUtils.getDigits(lxScope[1]));
            Double ljScope3 = Double.valueOf(StringUtils.getDigits(ljScope[0]));
            Double ljScope4 = Double.valueOf(StringUtils.getDigits(ljScope[1]));

            String[] bArr = v2.split("-");
            Double b1 = Double.valueOf(StringUtils.getDigits(bArr[0]));
            Double b2 = Double.valueOf(StringUtils.getDigits(bArr[1]));

            if (lxScope1<=b1 && lxScope2>b1 && ljScope3<=b2 && ljScope4>b2) {
                flag = true;
            }
        }

        return flag;
    }

    //B＞=A
    public static  Boolean getCommonCheck203(String v1, String v2) {
        boolean flag = false;
        if (StringUtils.isNotEmpty(v1) && StringUtils.isNotEmpty(v2)) {
            BigDecimal num1 = new BigDecimal(v1);
            BigDecimal num2 = new BigDecimal(v2);
            if (num2.compareTo(num1) < 0) {
                //System.out.println(num2 + " 小于 " + num1);
            } else if (num2.compareTo(num1) > 0) {
                //System.out.println(num2 + " 大于 " + num1);
                flag = true;
            } else {
                flag = true;
                //System.out.println(num2 + " 等于 " + num1);
            }
        }
//        if (v1.startsWith("-1")){
////            String[] parts = v1.split("-(?=.*-)");
////            String p = parts[1];
////            String one = "-1";
////            String two = p.split("-")[1];
////            Double a1 = Double.valueOf(StringUtils.getDigits(one));
////            Double a2 = Double.valueOf(StringUtils.getDigits(two));
////            if (a1<=b && a2>=b) {
////                flag = true;
////            }
//        } else {
////            Double a = Double.valueOf(StringUtils.getDigits(v1));
////            Double b = Double.valueOf(StringUtils.getDigits(v2));
////            if (b>=a) {
////                flag = true;
////            }
//        }
        return flag;
    }

    //B＜=A
    public static  Boolean getCommonCheck204(String v1, String v2) {
        boolean flag = false;
        if (StringUtils.isNotEmpty(v1) && StringUtils.isNotEmpty(v2)) {
            Double a = Double.valueOf(StringUtils.getDigits(v1));
            Double b = Double.valueOf(StringUtils.getDigits(v2));
            if (b<=a){
                flag = true;
            }
        }
        return flag;
    }

    //B＜A
    public static  Boolean getCommonCheck205(String v1, String v2) {
        boolean flag = false;
        if (StringUtils.isNotEmpty(v1) && StringUtils.isNotEmpty(v2)) {
            Double a = Double.valueOf(StringUtils.getDigits(v1));
            Double b = Double.valueOf(StringUtils.getDigits(v2));
            if (b<a){
                flag = true;
            }
        }
        return flag;
    }

    //B>A
    public static  Boolean getCommonCheck206(String v1, String v2) {
        boolean flag = false;
        if (StringUtils.isNotEmpty(v1) && StringUtils.isNotEmpty(v2)) {
            Double a = Double.valueOf(StringUtils.getDigits(v1));
            Double b = Double.valueOf(StringUtils.getDigits(v2));
            if (b>a){
                flag = true;
            }
        }
        return flag;
    }



    //B在A范围内 or A=不限   ||  A=B or A=不限    并小程序端多选
    public static Boolean getCommonCheck207(String v1, String v2) {
        boolean flag = false;
        if (StringUtils.isNotEmpty(v1) && StringUtils.isNotEmpty(v2)) {
            String[] arr = v1.split(",");
            endLoop:
            for (String s : arr) {
                if (v2.contains(s)) {
                    flag = true;
                    break endLoop;
                }
            }
        }
        return flag;
    }

    //A=不限 or （B≠不接受 and B在A范围内）
    public static Boolean getCommonCheck208(String v1, String v2) {
        boolean flag = false;
        if (StringUtils.isNotEmpty(v1) && StringUtils.isNotEmpty(v2)) {
            String[] arr = v1.split(",");
            endLoop:
            for (String s : arr) {
                if (!v2.equals("不接受") && !v2.equals("无") && v2.contains(s)) {
                    flag = true;
                    break endLoop;
                }
            }
        }
        return flag;
    }


    //
    public static Boolean getCommonLikeCheck(String v1, String v2) {
        boolean flag = false;
        String[] arr = v1.split(",");
        endLoop:
        for (String s : arr) {
            if (v2.contains(s) && !v2.equals("不接受")) {
                flag = true;
                break endLoop;
            }
        }

        return flag;
    }


    public static boolean isInRange(int num, int min, int max) {
        return num >= min && num <= max;
    }

    private static int extractMonths(String period) {
        // 提取第一个数字作为月份
        String monthsString = period.split("个")[0].replaceAll("[^0-9]", "");
        return Integer.parseInt(monthsString);
    }

}
